package view.component
{
	import enums.main.UISize;
	
	import feathers.controls.Button;
	import feathers.controls.ScrollContainer;
	import feathers.controls.Scroller;
	
	import starling.events.Event;
	
	import util.FG2FUGenerator;
	
	import view.render.BaseScrollerItem;
	
	/**
	 * 带scrollerContainer的ListView基类
	 * @author £iang
	 * 
	 */	
	public class BaseSCListView extends BaseListView
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 标题切换容器 */
		public var scollerContainer:ScrollContainer;
		/** 向左切换Button */
		public var leftButton:Button
		/** 向右切换Button */
		public var rightButton:Button
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** scollerContainer页面迭代器 */
		protected var _scrollerPageIter:int = 0;
		/** scollerContainer最大页数 */
		protected var _scrollerMaxPage:int;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initialize():void
		{ 
			super.initialize();
		}
		
		override protected function initializeOther():void
		{
			scollerContainer = new ScrollContainer();
			scollerContainer.scrollerProperties.hasElasticEdges = false;
			scollerContainer.scrollerProperties.snapToPages = true;
			scollerContainer.scrollerProperties.throwEase = "easeOut";
			scollerContainer.scrollerProperties.interactionMode = 
				Scroller.INTERACTION_MODE_MOUSE;
			scollerContainer.scrollerProperties.scrollBarDisplayMode = 
				Scroller.SCROLL_BAR_DISPLAY_MODE_NONE;
			
			titleGroup.addChild( scollerContainer );
			
			leftButton = FG2FUGenerator.generateRoundRectButton(
				40, UISize.LIST_TITLE_H, this.dpiScale, 12, "small"	
			);
			leftButton.label = "L";
			leftButton.isEnabled = false;
			titleGroup.addChild( leftButton );
			
			rightButton = FG2FUGenerator.generateRoundRectButton(
				40, UISize.LIST_TITLE_H, this.dpiScale, 12, "small"	
			);
			rightButton.label = "R";
			titleGroup.addChild( rightButton );
		}
		
		override protected function addListener():void
		{
			leftButton.addEventListener( Event.TRIGGERED, leftButtonTriggeredHandler );
			rightButton.addEventListener( Event.TRIGGERED, rightButtonTriggeredHandler );
		}
		
		
		/**
		 * 改变item中的 ScrollerContainer的pageIndex 
		 * @param pageIndex pageIndex
		 * 
		 */		
		protected function changeItemScrollerPage():void
		{
			var len:int = _listData.length;
			
			for ( var i:int = 0; i < len; i ++ )
			{
				( _listData.data[ i ].item as BaseScrollerItem ).changeScrollerPage( _scrollerPageIter );
			}
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理点击向左按钮后派发的事件 
		 * @param event 点击向左按钮后派发的事件 
		 * 
		 */		
		protected function leftButtonTriggeredHandler( event:Event ):void
		{
			_scrollerPageIter --;
			scollerContainer.scrollToPageIndex( _scrollerPageIter, 0, 0.5 );
			rightButton.isEnabled = true;
			changeItemScrollerPage();
			
			if ( _scrollerPageIter == 0 )
			{
				leftButton.isEnabled = false;
			}
		}
		
		/**
		 * 处理点击向右按钮后派发的事件 
		 * @param event 点击向右按钮后派发的事件 
		 * 
		 */	
		protected function rightButtonTriggeredHandler( event:Event ):void
		{
			_scrollerPageIter ++;
			scollerContainer.scrollToPageIndex( _scrollerPageIter, 0, 0.5 );
			leftButton.isEnabled = true;
			changeItemScrollerPage();
			
			if ( _scrollerPageIter == _scrollerMaxPage )
				rightButton.isEnabled = false;
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override protected function disposeOther():void
		{
			leftButton.dispose();
			FG2FUGenerator.deepDisposeButtonComp( leftButton );
			leftButton = null;
			rightButton.dispose();
			FG2FUGenerator.deepDisposeButtonComp( rightButton );
			rightButton = null;
			disposeAnythingInContainer();
			scollerContainer.dispose();
			scollerContainer = null;
		}
		
		/**
		 * 清除container中的组件 
		 * 
		 */		
		protected function disposeAnythingInContainer():void
		{
			
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function BaseSCListView()
		{
			super();
		}
	}
}